package com.xwltz.core.domain;

import com.xwltz.core.constants.HttpStatus;
import com.xwltz.core.defines.DescriptionField;

import java.util.HashMap;

/**
 * @description AjaxResult
 * @information on version 1.0 be created by @xwltz at 2021/11/22 18:06
 */

@SuppressWarnings("unused")
public class SimpleActionResult extends HashMap<String, Object> {
	@DescriptionField("状态码")
	public static final String CODE_TAG = "code";

	@DescriptionField("返回内容")
	public static final String MSG_TAG = "msg";

	@DescriptionField("数据对象")
	public static final String DATA_TAG = "data";

	@DescriptionField("初始化一个新创建的 AjaxResult 对象，使其表示一个空消息。")
	public SimpleActionResult() {
	}

	/**
	 * 初始化一个新创建的 AjaxResult 对象
	 *
	 * @param code 状态码
	 * @param msg  返回内容
	 */
	public SimpleActionResult(int code, String msg) {
		super.put(CODE_TAG, code);
		super.put(MSG_TAG, msg);
	}

	/**
	 * 初始化一个新创建的 AjaxResult 对象
	 *
	 * @param code 状态码
	 * @param msg  返回内容
	 * @param data 数据对象
	 */
	public SimpleActionResult(int code, String msg, Object data) {
		super.put(CODE_TAG, code);
		super.put(MSG_TAG, msg);
		if (data != null) {
			super.put(DATA_TAG, data);
		}
	}

	public static SimpleActionResult result(boolean success) {
		return success ? SimpleActionResult.success() : SimpleActionResult.error();
	}

	public static SimpleActionResult result(boolean success, String msg) {
		return success ? SimpleActionResult.success(msg) : SimpleActionResult.error(msg);
	}

	public static SimpleActionResult result(boolean success, Object data) {
		return success ? SimpleActionResult.success(data) : SimpleActionResult.error("fail");
	}

	public static SimpleActionResult result(boolean success, String msg, Object data) {
		return success ? SimpleActionResult.success(msg, data) : SimpleActionResult.error(msg, data);
	}

	/**
	 * 返回成功消息
	 *
	 * @return 成功消息
	 */
	public static SimpleActionResult success() {
		return SimpleActionResult.success("操作成功");
	}

	/**
	 * 返回成功数据
	 *
	 * @return 成功消息
	 */
	public static SimpleActionResult success(Object data) {
		return SimpleActionResult.success("操作成功", data);
	}

	/**
	 * 返回成功消息
	 *
	 * @param msg 返回内容
	 * @return 成功消息
	 */
	public static SimpleActionResult success(String msg) {
		return SimpleActionResult.success(msg, null);
	}

	/**
	 * 返回成功消息
	 *
	 * @param msg  返回内容
	 * @param data 数据对象
	 * @return 成功消息
	 */
	public static SimpleActionResult success(String msg, Object data) {
		return new SimpleActionResult(HttpStatus.HTTP_OK, msg, data);
	}

	/**
	 * 返回错误消息
	 */
	public static SimpleActionResult error() {
		return SimpleActionResult.error("操作失败");
	}

	/**
	 * 返回错误消息
	 *
	 * @param msg 返回内容
	 * @return 警告消息
	 */
	public static SimpleActionResult error(String msg) {
		return SimpleActionResult.error(msg, null);
	}

	/**
	 * 返回错误消息
	 *
	 * @param msg  返回内容
	 * @param data 数据对象
	 * @return 警告消息
	 */
	public static SimpleActionResult error(String msg, Object data) {
		return new SimpleActionResult(HttpStatus.HTTP_INTERNAL_ERROR, msg, data);
	}

	/**
	 * 返回错误消息
	 *
	 * @param code 状态码
	 * @param msg  返回内容
	 * @return 警告消息
	 */
	public static SimpleActionResult error(int code, String msg) {
		return new SimpleActionResult(code, msg, null);
	}

	/**
	 * 方便链式调用
	 *
	 * @param key   键
	 * @param value 值
	 * @return 数据对象
	 */
	@Override
	public SimpleActionResult put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
